package com.poor.user.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.poor.user.service.core.Result;
import com.poor.user.service.core.ResultGenerator;
import com.poor.user.service.model.PoorUser;
import com.poor.user.service.service.PoorUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by lcy on 2018/12/21.
*/
@RestController
@RequestMapping("/poor/user")
public class PoorUserController {
    @Resource
    private PoorUserService poorUserService;

    @PostMapping("/add")
    public Result add(@RequestBody PoorUser poorUser) {
        poorUserService.save(poorUser);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        poorUserService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody PoorUser poorUser) {
        poorUserService.update(poorUser);
        return ResultGenerator.genSuccessResult();
    }
    @PostMapping("/findByWxUnionId")
    public Result findByWxUnionId(@RequestBody PoorUser poorUser) {
        return ResultGenerator.genSuccessResult(poorUserService.findByWxUnionId(poorUser));
    }
    @PostMapping("/detail")
    public Result detail(@RequestBody PoorUser poorUser) {
        PoorUser responsePoorUser = poorUserService.findById(poorUser.getUserNo());
        return ResultGenerator.genSuccessResult(responsePoorUser);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<PoorUser> list = poorUserService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
